package com.icongyou.enterprise.talent_analysis.mapper;

import com.icongyou.enterprise.talent_analysis.entity.PortraitCache;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;

/**
 * 画像缓存Mapper测试
 */
@SpringBootTest
@Transactional
public class PortraitCacheMapperTest {

    @Autowired
    private PortraitCacheMapper mapper;

    // 使用数据库中存在的学生ID
    private static final String STUDENT_1 = "660e8400-e29b-41d4-a716-446655440101"; // 张三
    private static final String STUDENT_2 = "660e8400-e29b-41d4-a716-446655440102"; // 李四
    private static final String STUDENT_3 = "660e8400-e29b-41d4-a716-446655440103"; // 王五
    private static final String STUDENT_4 = "660e8400-e29b-41d4-a716-446655440104"; // 赵六
    
    private static final String TEMPLATE_1 = "130e8400-e29b-41d4-a716-446655441301"; // 互联网技术岗位模板

    @BeforeEach
    public void setUp() {
        // 清理测试学生的缓存数据,确保测试独立性
        mapper.deleteByStudentId(STUDENT_1);
        mapper.deleteByStudentId(STUDENT_2);
        mapper.deleteByStudentId(STUDENT_3);
        mapper.deleteByStudentId(STUDENT_4);
    }

    @Test
    public void testInsertAndSelect() {
        PortraitCache cache = createCache(STUDENT_1, TEMPLATE_1);
        
        int result = mapper.insert(cache);
        assertEquals(1, result);

        PortraitCache retrieved = mapper.selectByStudentAndTemplate(STUDENT_1, TEMPLATE_1);
        assertNotNull(retrieved);
        assertEquals(STUDENT_1, retrieved.getStudentId());
        assertEquals(TEMPLATE_1, retrieved.getTemplateId());
        assertNotNull(retrieved.getLabelsJson());
        assertNotNull(retrieved.getScoresJson());
    }

    @Test
    public void testSelectByStudentAndTemplate_withNullTemplate() {
        PortraitCache cache = createCache(STUDENT_2, null);
        mapper.insert(cache);

        PortraitCache retrieved = mapper.selectByStudentAndTemplate(STUDENT_2, null);
        assertNotNull(retrieved);
        assertEquals(STUDENT_2, retrieved.getStudentId());
        assertNull(retrieved.getTemplateId());
    }

    @Test
    public void testSelectByStudentAndTemplate_notFound() {
        PortraitCache retrieved = mapper.selectByStudentAndTemplate("nonexistent", "nonexistent");
        assertNull(retrieved);
    }

    @Test
    public void testUpdateByIdWithVersion() {
        PortraitCache cache = createCache(STUDENT_3, TEMPLATE_1);
        cache.setVersion(1);
        mapper.insert(cache);

        // 更新
        cache.setLabelsJson("[\"技术能力\",\"团队协作\"]");
        cache.setScoresJson("[90,85]");
        cache.setDimensionsJson("{\"updated\":true}");
        cache.setUpdatedAt(LocalDateTime.now());
        
        int result = mapper.updateByIdWithVersion(cache);
        assertEquals(1, result);

        PortraitCache updated = mapper.selectByStudentAndTemplate(STUDENT_3, TEMPLATE_1);
        assertNotNull(updated);
        assertTrue(updated.getDimensionsJson().contains("updated"));
        assertEquals(2, updated.getVersion()); // 版本号应该增加
    }

    @Test
    public void testUpdateByIdWithVersion_versionConflict() {
        PortraitCache cache = createCache(STUDENT_4, TEMPLATE_1);
        cache.setVersion(1);
        mapper.insert(cache);

        // 使用错误的版本号更新
        cache.setVersion(999); // 错误的版本号
        cache.setDimensionsJson("{\"wrong\":true}");
        
        int result = mapper.updateByIdWithVersion(cache);
        assertEquals(0, result); // 应该更新失败
    }

    @Test
    public void testDeleteByStudentId() {
        // 为同一学生插入多个模板的缓存
        mapper.insert(createCache(STUDENT_1, TEMPLATE_1));
        mapper.insert(createCache(STUDENT_1, null));

        int result = mapper.deleteByStudentId(STUDENT_1);
        assertEquals(2, result);

        // 验证已删除
        assertNull(mapper.selectByStudentAndTemplate(STUDENT_1, TEMPLATE_1));
        assertNull(mapper.selectByStudentAndTemplate(STUDENT_1, null));
    }

    @Test
    public void testDeleteByTemplateId() {
        // 先清理可能存在的数据
        mapper.deleteByTemplateId(TEMPLATE_1);
        
        // 为多个学生插入同一模板的缓存
        mapper.insert(createCache(STUDENT_1, TEMPLATE_1));
        mapper.insert(createCache(STUDENT_2, TEMPLATE_1));
        mapper.insert(createCache(STUDENT_3, TEMPLATE_1));

        int result = mapper.deleteByTemplateId(TEMPLATE_1);
        assertEquals(3, result);

        // 验证已删除
        assertNull(mapper.selectByStudentAndTemplate(STUDENT_1, TEMPLATE_1));
        assertNull(mapper.selectByStudentAndTemplate(STUDENT_2, TEMPLATE_1));
    }

    @Test
    public void testDeleteByStudentId_noData() {
        int result = mapper.deleteByStudentId("nonexistent-student");
        assertEquals(0, result);
    }

    @Test
    public void testDeleteByTemplateId_noData() {
        int result = mapper.deleteByTemplateId("nonexistent-template");
        assertEquals(0, result);
    }

    // ==================== Helper Methods ====================

    private PortraitCache createCache(String studentId, String templateId) {
        PortraitCache cache = new PortraitCache();
        cache.setId(UUID.randomUUID().toString());
        cache.setStudentId(studentId);
        cache.setTemplateId(templateId);
        cache.setLabelsJson("[\"技术能力\",\"问题解决\"]");
        cache.setScoresJson("[80,75]");
        cache.setDimensionsJson("{\"dimensions\":[],\"scores\":{}}");
        cache.setAiUsed(false);
        cache.setGeneratedAt(LocalDateTime.now());
        cache.setUpdatedAt(LocalDateTime.now());
        cache.setVersion(1);
        return cache;
    }
}
